package com.mango.coomb.threads;


import com.mango.coomb.constant.ThreadConst;
import com.mango.coomb.enums.AreaClass;
import com.mango.coomb.enums.Sign;
import com.mango.coomb.enums.Status;
import com.mango.coomb.enums.ThreadClass;
import com.mango.coomb.threads.parameter.ThreadParameter;
import com.mango.coomb.threads.parameter.util.ThreadParamUpdateUtil;

public class ReaderZeroRead extends Thread {
    private static final Integer[] bufferBufferValueArr = ThreadParameter.getBufferBufferValueArr();
    private static final Integer[] readerZeroBufferValueArr = ThreadParameter.getReaderZeroBufferValueArr();
    private Status status = Status.NOT_BEGIN;
    private boolean paused=false;
    @Override
    public void run() {
        while (ThreadParameter.isReaderZeroReadRunning) {
            while (!this.paused) {
                int j = ThreadParamUpdateUtil.searchNull(AreaClass.READER, Sign.READER_ZERO);
                if (j == ThreadConst.NOT_SEARCH) {
                    this.status = Status.PAUSED;
                    try {
                        synchronized (readerZeroBufferValueArr) {
                            readerZeroBufferValueArr.wait();
                        }
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                } else {
                    this.status = Status.PAUSED;
                    Integer value = null;
                    int i = ThreadParamUpdateUtil.searchNotNull(AreaClass.BUFFER, Sign.READER_ZERO);
                    if (i == ThreadConst.NOT_SEARCH) {
                        ThreadParameter.isReaderZeroBufferLockedArr[j] = false;
                        this.status = Status.PAUSED;
                        try {
                            synchronized (bufferBufferValueArr) {
                                bufferBufferValueArr.wait();
                            }
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        continue;
                    } else {
                        this.status = Status.PAUSED;
                        synchronized (ThreadParameter.bufferBufferLock[i]) {
                            this.status = Status.RUNNING;
//                            ThreadParameter.isBufferBufferLockedArr[i] = true;
                            try {
                                Thread.sleep(1500L / ThreadParameter.getSpeedMultiple(ThreadClass.READER));
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                            value = bufferBufferValueArr[i];
                            ThreadParamUpdateUtil.updateBufferBufferValueArr(i, null);
                            ThreadParameter.isBufferBufferLockedArr[i] = false;
                            this.status = Status.NOT_BEGIN;
                        }
                        synchronized (bufferBufferValueArr) {
                            bufferBufferValueArr.notifyAll();
                        }
                    }
                    synchronized (ThreadParameter.readerZeroBufferLock[j]) {
                        this.status = Status.RUNNING;
//                        ThreadParameter.isReaderZeroBufferLockedArr[j] = true;
                        try {
                            Thread.sleep(1500L / ThreadParameter.getSpeedMultiple(ThreadClass.READER));
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        ThreadParamUpdateUtil.updateReaderZeroBufferValueArr(j, value);
                        ThreadParameter.isReaderZeroBufferLockedArr[j] = false;
                        this.status = Status.NOT_BEGIN;
                    }
                    synchronized (readerZeroBufferValueArr) {
                        readerZeroBufferValueArr.notifyAll();
                    }
                }

            }
//            this.status = Status.PAUSED;
            try {
                synchronized (ThreadParameter.readerZeroReadLock) {
                    ThreadParameter.readerZeroReadLock.wait();
                }
//                synchronized (ThreadParameter.totalLock){
//                    ThreadParameter.totalLock.wait();
//                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
////            Thread.interrupted();
//            this.status = Status.NOT_BEGIN;
        }
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public boolean isPaused() {
        return paused;
    }

    public void setPaused(boolean paused) {
        this.paused = paused;
    }
}
